package com.tutu.controller.backend;

import com.tutu.common.DataTableFactory;
import com.tutu.controller.common.CommonController;
import com.tutu.service.ModuleService;
import com.tutu.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 菜单管理
 */
@Controller
@RequestMapping(value = "backend/menu")
public class MenuController extends CommonController {

    @Autowired
    ModuleService moduleService;

    @RequestMapping(value = "/index")
    public String index() {
        return "backend/menu/list";
    }

    @RequestMapping("/list")
    public void list(HttpServletResponse response, String keyWord, Integer draw, Integer start, Integer length) {
        int pageNum = getPageNum(start, length);

        WebUtil.printJson(response, DataTableFactory.fitting(draw, moduleService.pageByParams(keyWord, pageNum, length)));
    }

    @RequestMapping("/save")
    @ResponseBody
    public Map<String, Object> save(HttpServletRequest request, Integer id, String name, String liName, String description, Integer sortIndex) {
        return moduleService.saveInfo(request, id, name, liName, description, sortIndex);
    }

    @RequestMapping("/del")
    @ResponseBody
    public Map<String, Object> del(HttpServletRequest request, Integer id) {
        return moduleService.deleteById(request, id);
    }

    @RequestMapping("/batchDel")
    @ResponseBody
    public Map<String, Object> batchDel(HttpServletRequest request, String ids) {
        return moduleService.batchDel(request, ids);
    }
}
